//
//  BLTimeAxisRuleRenderer.m
//  BLTimeAxis
//
//  Created by dg on 2025/4/18.
//

#import "BLTimeAxisRuleRenderer.h"

@implementation BLTimeAxisRuleRenderer

- (void)visitTimeAxisData:(BLTimeAxisData *)aTimeAxisData {
    if (![aTimeAxisData isKindOfClass:[BLTimeAxisData class]]) return ;
    
    if (aTimeAxisData.startTimeInterval > self.maxTimeInVisible
        || aTimeAxisData.endTimeInterval < self.minTimeInVisible) {
        return ;
    }
    
    [super visitTimeAxisData:aTimeAxisData];
    if (self.axisDirection == BLTimeAxisDirectionHorizontal) {
        [self drawLineWithContext:self.context from:CGPointMake((aTimeAxisData.startTimeInterval - self.minTimeInVisible) * self.aSecondOfPixel, aTimeAxisData.strokeSize/2.0) to:CGPointMake((aTimeAxisData.endTimeInterval - self.minTimeInVisible) * self.aSecondOfPixel, aTimeAxisData.strokeSize/2.0)];
    } else {
        [self drawLineWithContext:self.context from:CGPointMake((self.viewHeight-aTimeAxisData.strokeSize)/2.0, (aTimeAxisData.startTimeInterval - self.minTimeInVisible) * self.aSecondOfPixel) to:CGPointMake((self.viewHeight-aTimeAxisData.strokeSize)/2.0, (aTimeAxisData.endTimeInterval - self.minTimeInVisible) * self.aSecondOfPixel)];
    }
}

/// 绘制时间轴每段
- (void)visitTimeAxisDigitalDivision:(BLTimeAxisDigitalDivision *)aTimeAxisDigitalDivision {
    
    [super visitTimeAxisDigitalDivision:aTimeAxisDigitalDivision];
    
    CGFloat aSecondWidth = self.aSecondOfPixel;
    CGFloat aHourWidth = aSecondWidth * 3600.0;
    CGFloat ruleOffset = self.ruleFixedOffset;
    CGFloat baseLineOffset = self.baseLineFixedOffset;
    NSInteger currentHour = self.currentHour;
    CGFloat maxWidth = self.axisDirection == BLTimeAxisDirectionHorizontal ? self.viewWidth : self.viewHeight;
    
    // 计算当前时间的所在的整点小时时间的差值
    NSTimeInterval diffTime = self.currentTimeInterval - (currentHour * 3600);// 当前时间距离正点的差值
        
    CGFloat tempX = ruleOffset - diffTime * aSecondWidth;
    // 计算最左边的小时数，一般要画到视图外多一个小时
    NSInteger count = 1;
    while ((tempX -= aHourWidth) > 0) {
        count++;
    }
    NSInteger minHour = currentHour - count;
    CGFloat minHourPosition = ruleOffset - (diffTime+60*60*count) * aSecondWidth;
    
    // 计算最右边的小时数，一般要画到视图外多一个小时
    count = 1;
    while ((tempX += aHourWidth) < maxWidth) {
        count++;
    }
    CGFloat maxHourPosition = ruleOffset + (diffTime+60*60*count) * aSecondWidth;
    
    // 开始画刻度
    CGFloat tempHourPosition = minHourPosition;
    NSInteger tempHour = minHour;
    
    BOOL isHigh = NO;
    
    while (tempHourPosition <= maxHourPosition) {
        
        if (tempHour >= 0 && tempHour <= 23) {
            CGPoint point = CGPointMake(tempHourPosition, baseLineOffset);
            // 文字显示
            NSString *time = [NSString stringWithFormat:@"%02zd:00",tempHour > 23 ? tempHour-24 : (tempHour < 0 ? tempHour + 24: tempHour)];
            [time drawInRect:CGRectMake(point.x-13, point.y+5, 40, 20) withAttributes:aTimeAxisDigitalDivision.digitalAttribute];
            
            // 大刻度线(小时)
            CGContextMoveToPoint(self.context, point.x-.5, point.y);
            CGContextAddLineToPoint(self.context, point.x-.5, point.y-20.0);
            CGContextStrokePath(self.context);
            
            // 小刻度线(10分钟)
            int piece = 12;
            for (int i = 1; i < piece; i++) {
                CGPoint point = CGPointMake(aHourWidth/piece*i+tempHourPosition, baseLineOffset);
                CGContextMoveToPoint(self.context, point.x-.5, point.y);
                CGContextAddLineToPoint(self.context, point.x-.5, point.y-10.0);
                CGContextStrokePath(self.context);
            }
        }
        
        if (tempHour == 24 && isHigh == NO) {
            CGPoint point = CGPointMake(tempHourPosition, baseLineOffset);
            
            NSString *time = [NSString stringWithFormat:@"00:00"];
            [time drawInRect:CGRectMake(point.x-13, point.y+5, 40, 20) withAttributes:aTimeAxisDigitalDivision.digitalAttribute];
            // 大刻度线(小时)
            CGContextMoveToPoint(self.context, point.x-.5, point.y);
            CGContextAddLineToPoint(self.context, point.x-.5, point.y-20.0);
            CGContextStrokePath(self.context);
            isHigh = YES;
        }
        
        tempHourPosition += aHourWidth;
        tempHour++;
    }
}


- (void)visitTimeAxisRule:(BLTimeAxisRule *)aTimeAxisRule {
    [super visitTimeAxisRule:aTimeAxisRule];
    
    if (self.axisDirection == BLTimeAxisDirectionHorizontal) {
        [self drawLineWithContext:self.context from:CGPointMake(aTimeAxisRule.fixedOffset, 0) to:CGPointMake(aTimeAxisRule.fixedOffset, self.viewHeight)];
    } else {
        [self drawLineWithContext:self.context from:CGPointMake(0, aTimeAxisRule.fixedOffset) to:CGPointMake(self.viewWidth, aTimeAxisRule.fixedOffset)];
    }
}

- (void)visitTimeAxisBaseLine:(BLTimeAxisBaseLine *)aTimeAxisBaseLine {
    [super visitTimeAxisBaseLine:aTimeAxisBaseLine];
    
    CGFloat aSecondWidth = self.aSecondOfPixel;
    CGFloat x = 0.0;
    CGFloat dstX = self.viewWidth;
    if ((2 * 3600) > self.currentTimeInterval) {
        x = (self.viewWidth/2) - (self.currentTimeInterval * aSecondWidth);
        x = x < 0 ? 0 : x;
    }
    
    if (self.currentTimeInterval > (22 * 3600)) {
        dstX = (self.viewWidth/2) + ((86400 - self.currentTimeInterval) * aSecondWidth);
        dstX = dstX > self.viewWidth ? self.viewWidth : dstX;
    }

    [self drawLineWithContext:self.context from:CGPointMake(x, aTimeAxisBaseLine.fixedOffset - aTimeAxisBaseLine.strokeSize/2.0)
                           to:CGPointMake(dstX, aTimeAxisBaseLine.fixedOffset - aTimeAxisBaseLine.strokeSize/2.0)];
}

- (void)visitTimeAxisBackground:(BLTimeAxisBackground *)aTimeAxisBackground {
    
}


@end
